package org.jetlinks.community.standalone.entry;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.jetlinks.community.device.entity.DeviceInstanceEntity;
import org.jetlinks.community.device.entity.DeviceProductEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class ThingsJsDeviceDto {
    //孪生体id（不可重复)
    private String twinId;
    //孪生体的名称
    private String name;
    //分类名
    private String classname;
    //分类id
    private String productId;
    // 孪生体数据（静态资产类属性和动态指标类属性）
    private List<Map<String , Object>> attrs;

    private List<String> keys;

    public ThingsJsDeviceDto with(DeviceProductEntity deviceProduct, DeviceInstanceEntity device){
        setTwinId(device.getId());
        setName(device.getName());
        setClassname(deviceProduct.getId());
        setProductId(device.getProductId());
        List<Map<String , Object>> list = new ArrayList<>();
        JSONObject model = JSONObject.parseObject(deviceProduct.getMetadata());
        JSONArray properties = model.getJSONArray("properties");
        setKeys(properties.stream().map(propertie->((JSONObject)propertie).getString("id")).collect(Collectors.toList()));
        for (int i = 0; i < properties.size(); i ++ ) {
            JSONObject propertie = properties.getJSONObject(i);
            Map<String, Object> map = new HashMap<>();
            map.put("key" , propertie.getString("id"));
            map.put("value" , "10");
            list.add(map);
        }
        setAttrs(list);
        return this;
    }
    public ThingsJsDeviceDto with(DeviceInstanceEntity device){
        setTwinId(device.getId());
        setName(device.getName());
        setProductId(device.getProductId());
        return this;
    }
}
